package com.heitian.ssm.controller;

import cc.openkit.common.KitUtil;
import com.alibaba.fastjson.JSONObject;
import com.heitian.ssm.model.AddressList;
import com.heitian.ssm.model.CallRecord;
import com.heitian.ssm.service.CallRecordService;
import com.heitian.ssm.util.Pager;
import com.heitian.ssm.util.StaticFinalVar;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/callRecord")
public class CallRecordController {
	private Logger log = Logger.getLogger(CallRecordController.class);

	@Resource
	private CallRecordService callRecordService;

	/**
	 * 添加
	 *
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	public Object add(HttpServletRequest request) {
		log.info("添加通话记录");
		String callId = request.getParameter("callId");//通话id
		String place = request.getParameter("place");//座机号
		String number = request.getParameter("number");//主叫号码
		String type = request.getParameter("type");//主叫号码
		//request.getParameter("soundUrl");//录音url
		String soundUrl = null;

		CallRecord callRecord = new CallRecord();
		if (KitUtil.feikong(callId)) {
			callRecord.setCallId(callId);
		}
		if (KitUtil.feikong(place)) {
			callRecord.setPlace(place);
		}
		if (KitUtil.feikong(number)) {
			callRecord.setNumber(number);
		}
		if (KitUtil.feikong(soundUrl)) {
			callRecord.setSoundUrl(soundUrl);
		}
		if (KitUtil.feikong(type)) {
			callRecord.setType(Integer.parseInt(type));
		}
		callRecord.setId(KitUtil.uuid());
		callRecord.setTime(new Date());
		callRecord.setStatus(0);

		return JSONObject.toJSON(callRecordService.save(callRecord) == 1 ?
				KitUtil.returnMap("200", StaticFinalVar.ADD_OK) :
				KitUtil.returnMap("101", StaticFinalVar.ADD_ERR));
	}

	/**
	 * 添加
	 *
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/answer", method = RequestMethod.POST)
	@ResponseBody
	public Object answer(HttpServletRequest request) {
		log.info("添加通话记录");
		String callId = request.getParameter("callId");//通话id
		//String status = request.getParameter("status");//录音url

		CallRecord callRecord = new CallRecord();
		if (KitUtil.feikong(callId)) {
			callRecord.setCallId(callId);
		}
		callRecord.setStatus(1);

		return JSONObject.toJSON(callRecordService.answer(callRecord) == 1 ?
				KitUtil.returnMap("200", StaticFinalVar.ADD_OK) :
				KitUtil.returnMap("101", StaticFinalVar.ADD_ERR));
	}

	@RequestMapping("/getAllJson")
	@ResponseBody
	public Object getAllJson(HttpServletRequest request) {
		log.info("获取通讯录数据");
		String place = request.getParameter("place");
		String startTime = request.getParameter("startTime");
		String endTime = request.getParameter("endTime");


		if (KitUtil.feikong(startTime)) {
			startTime = startTime + " 00:00:00";
		}
		if (KitUtil.feikong(endTime)) {
			endTime = endTime + " 23:59:59";
		}
		List<CallRecord> list = callRecordService.getCallRecordList(place, startTime, endTime);

		Map<String, Object> map = new HashMap<String, Object>();
		// 返回数据
		map.put("code", 200);
		map.put("msg", "");
		map.put("data", list);

		return JSONObject.toJSON(map);
	}
}
